<?php

require_once( dirname(__FILE__).'/../libs/bedmintonlib.php' );

$db = Db::db();
$dbConnection = $db->connection();
if (is_null( $dbConnection ))
{
    echo( $db->getErrorStr() );
    exit;
}

$isEmpty = true;
$resultStr = "<div class=\"gallerytableholder\"><table class = \"gallerytable\">";
try
{
    // obrazkom v galerii dat popisy + prelozit galeriu z anglictiny
    // kliknutim na home sa vratim sem
    $statement = $dbConnection->prepare( "SELECT id, gallerydate, description, comment FROM galleries ORDER BY gallerydate" );
    $statement->execute();
    $result = $statement->fetchAll( PDO::FETCH_NUM );
    if (! $result)
        exit;
    
    foreach($result as $row)
    {
        $existImages = false;
        $trStr = "";
        $trStr .= "<tr id =\"gallerytr" . $row[ 0 ] . "\" class=\"gallerytr\">";

        // gallery description params
        $trStr .= "<td> <div class=\"galleryDescriptionTable\"><table>";
        $trStr .= "<tr><td>" . $row[2] . " - " . MyDateTime::fromPqDate( $row[ 1 ] )->toDate() . "</td></tr>";
        $trStr .= "<tr><td>" . $row[3] . "</td></tr>";
        $trStr .= "</table></div></td>";
        
        // show first 3 images of the gallery
        $st = $dbConnection->prepare( "SELECT imagepreviewpath, description FROM galleryimages WHERE galleryid = ? LIMIT 3" );
        $st->bindParam( 1, $row[ 0 ] );
        $st->execute();
        $res = $st->fetchAll( PDO::FETCH_NUM );
        $counter = 0;
        if ($res)
        {
            foreach($res as $r)
            {
                $trStr .= "<td><table>";
                $trStr .= "<tr><td class =\"image\">" . "<img src=\"../galleryimages/$r[0]\" alt=\"\" />" . $r[1] ."</td></tr>";
                $trStr .= "</table></td>";
                $counter ++;
                $existImages = true;
            }
        }
        
        while ($counter < 3)
        {
           $trStr .= "<td class =\"image\">&nbsp</td>"; 
           $counter ++;
        }

        $trStr .= "</tr>";
        if ($existImages)
            $resultStr .= $trStr;
        
        $isEmpty = false;
    }
    $resultStr .= "</table></div>";
}
catch(PDOException $e)
{
    echo( $e->getMessage() );
    exit;
}

if ($isEmpty)
    exit;

$javascriptStr = "
<script type=\"text/javascript\">   
    $(\".gallerytr\").click(function()
    {
       idStr = $(this).attr( 'id' );
       id = idStr.substring( 9 );
       urlStr = 'index.php?siteName=mainsites/gallerydetails.php&galleryId=' + id;
       window.location.href = urlStr;
    });
</script>";

$resultStr .= $javascriptStr;
echo( $resultStr );

?>


